<?php
defined('IN_TS') or die('Access Denied.'); 
/*
 *QQ微博同步帖子插件
 *BY QiuJun 2011-07-27
 *实现功能：绑定QQ微博帐号后，同步发帖到微博，一键转帖到微博，用户评论为微博
 */

function qqt_html(){
	global $TS_USER;
	global $TS_SITE;
	
	echo '<h2>微博同步 &nbsp; ·&nbsp;·&nbsp;·&nbsp;·&nbsp;·&nbsp;·&nbsp;</h2>';
	echo '<div id="txWB_W1"></div><script type="text/javascript">var tencent_wb_name = "thinksaas";var tencent_wb_sign = "e5ed641a73f34719e7cf8a4aab95a2feec2a08b6";var tencent_wb_style = "3";</script><script type="text/javascript" src="http://v.t.qq.com/follow/widget.js" charset="utf-8"/></script>';
	
	//if(isset($_SESSION['last_key'])){
	if($TS_USER['user']['qqt_oauth_token'] != '' && $TS_USER['user']['qqt_oauth_token_secret'] != ''){
		echo '<div class="indent"><img title="QQ微博" src="plugins/group/qqt/weiboicon16.png" align="absmiddle" /> 已启用发帖同步到QQ微博</div>';
	}else{
		require_once('config.php');
		require_once('oauth.php');
		require_once('opent.php');
		
		$o = new MBOpenTOAuth( MB_AKEY , MB_SKEY  );
		$keys = $o->getRequestToken($TS_SITE["base"]["site_url"].'index.php?app=group&ac=plugin&plugin=qqt&in=callback');
		$aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false,'');
		$_SESSION['keys'] = $keys;
		
		echo '<div class="indent"><img title="QQ微博" src="plugins/group/qqt/weiboicon16.png" align="absmiddle" /> <a href="'.$aurl.'" title="启用发帖同步到QQ微博">未启用发帖同步到QQ微博</a></div>';
		echo '<div class="clear"></div>';
	}
	
}

addAction('my_right_top', 'qqt_html');

//发帖同步开始 
function qqt_send($title,$topicid,$TS_USER,$TS_SITE,$TS_URL){
	//if(isset($_SESSION['last_key'])){
	if($TS_USER['user']['qqt_oauth_token'] != '' && $TS_USER['user']['qqt_oauth_token_secret'] != ''){
		require_once('config.php');
		require_once('oauth.php');
		require_once('opent.php');
		require_once('api_client.php');
		
		//$c = new MBApiClient( MB_AKEY , MB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']);
		$c = new MBApiClient( MB_AKEY , MB_SKEY , $TS_USER['user']['qqt_oauth_token'] , $TS_USER['user']['qqt_oauth_token_secret']);
		
		$postData = $title.' - 来自'.$TS_SITE["base"]["site_title"].'：' . $TS_SITE["base"]["site_url"].$TS_URL["topic"].$topicid.$TS_URL["suffix"];
		
		$p =array(
			'c' => $postData,
			'ip' => $_SERVER['REMOTE_ADDR'], 
			'j' => '',
			'w' => ''
		);
		
		$ret = $c->postOne($p);
	}
	
}

addAction('topic_add', 'qqt_send');

//一键转播
function qqt_zb(){
	echo "<div><img src=\"http://v.t.qq.com/share/images/s/weiboicon16.png\" align=\"absmiddle\" border=\"0\" alt=\"转播到腾讯微博\" /> <a href=\"javascript:void(0)\" onclick=\"postToWb();return false;\" style=\"height:16px;font-size:12px;line-height:16px;\">转播到腾讯微博</a></div>
	<script type=\"text/javascript\">
	function postToWb(){
		var _t = encodeURI(document.title);
		var _url = encodeURIComponent(document.location);
		var _assname = encodeURI(\"\");
		var _appkey = encodeURI(\"665338350d65490c9bcff2be89ed35b1\");//你从腾讯获得的appkey
		var _pic = encodeURI('');//（例如：var _pic='图片url1|图片url2|图片url3....）
		var _site = '';//你的网站地址
		var _u = 'http://v.t.qq.com/share/share.php?url='+_url+'&appkey='+_appkey+'&site='+_site+'&pic='+_pic+'&title='+_t+'&assname='+_assname;
		window.open( _u,'', 'width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' );
	}
</script>

	";
}

addAction('topic_footer', 'qqt_zb');